﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;

namespace KWT.DRG_DIP.API.Infrastructure
{
    public class PageListResult : IActionResult
    {
        /// <summary>
        /// 列表数据
        /// </summary>
        public object List { get; set; }
        /// <summary>
        /// 总记录数
        /// </summary>
        public int Total { get; set; }

        public PageListResult(object _list, int _total)
        {
            List = _list;
            Total = _total;
        }

        public Task ExecuteResultAsync(ActionContext context)
        {
            var fmt = new IsoDateTimeConverter()
            {
                DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
            };
            var setting = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
                Converters = new List<JsonConverter> { fmt }
            };
            string json = JsonConvert.SerializeObject(new { List, Total, trace_id = context.HttpContext.TraceIdentifier }, setting);
            context.HttpContext.Response.ContentType = "application/json;charset=utf-8";
            return context.HttpContext.Response.WriteAsync(json);
        }
    }
}
